package com.xiu.concurrency;

class Sleeper extends Thread{
	private int duration;
	public Sleeper(String name,int sleepTime) {
		super(name);
		this.duration=sleepTime;
		start();
	}
	@Override
	public void run() {
		try {
			sleep(duration);
		} catch (InterruptedException e) {
			System.out.println(getName()+" was Interrupted .is Interrupted() "+isInterrupted());
			return;
		}
		System.out.println(getName()+" has awakened");
	}
}

class Joiner extends Thread{
	private Sleeper sleeper;
	public Joiner(Sleeper sleeper,String name) {
		super(name);
		this.sleeper=sleeper;
		start();
	}
	@Override
	public void run() {
		try {
			sleeper.join();
		} catch (InterruptedException e) {
			System.out.println("Interrupted");
		}
		System.out.println(getName()+ " join completed");
	}
}
public class Joining {

	public static void main(String[] args) {
		Sleeper sleepy=new Sleeper("sleepy", 1500);
		Sleeper grumpy=new Sleeper("grumpy", 1500);	
		Joiner dopey=new Joiner(sleepy, "dopey");
		Joiner doc=new Joiner( grumpy, "doc");
		grumpy.interrupt();
	}

}
